Replace dictionary values with their averageΒΆ
Replace dictionary values with their avarage.
def sum_math_v_vi_average(LOD):
for d in LOD:
n1 = d.pop('V')
n2 = d.pop('VI')
d['V + VI'] = (n1 + n2) / 2
return LOD
Test:
student_details = [
{'id': 1, 'subject': 'math', 'V': 70, 'VI': 82},
{'id': 2, 'subject': 'math', 'V': 73, 'VI': 74},
{'id': 3, 'subject': 'math', 'V': 75, 'VI': 86},
]
print(sum_math_v_vi_average(student_details))
Output:
[{'id': 1, 'subject': 'math', 'V + VI': 76.0},
{'id': 2, 'subject': 'math', 'V + VI': 73.5},
{'id': 3, 'subject': 'math', 'V + VI': 80.5}
]